package com.linngdu664.bettersnowballfight.itemclass;

import com.linngdu664.bettersnowballfight.Register;
import com.linngdu664.bettersnowballfight.entityclass.AdvancedSnowballEntity;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.stat.Stats;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.Formatting;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.util.UseAction;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class SnowballMachineGunItem extends Item {
    private static int timer;
    private static float j;

    public SnowballMachineGunItem(Settings settings) {
        super(settings);
    }

    @Override
    public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
        timer = 0;
        ItemStack stack = user.getStackInHand(hand);
        user.setCurrentHand(hand);
        user.incrementStat(Stats.USED.getOrCreateStat(this));
        return TypedActionResult.consume(stack);
    }

    @Override
    public UseAction getUseAction(ItemStack stack) {
        return UseAction.BOW;
    }

    @Override
    public int getMaxUseTime(ItemStack stack) {
        return Integer.MAX_VALUE;
    }

    protected boolean isAmmoTank(ItemStack stack) {
        return stack.getItem() == Register.COMPACTED_SNOWBALL_STORAGE_TANK || stack.getItem() == Register.EXPLOSIVE_SNOWBALL_STORAGE_TANK ||
                stack.getItem() == Register.GLASS_SNOWBALL_STORAGE_TANK || stack.getItem() == Register.GOLD_SNOWBALL_STORAGE_TANK ||
                stack.getItem() == Register.ICE_SNOWBALL_STORAGE_TANK || stack.getItem() == Register.IRON_SNOWBALL_STORAGE_TANK ||
                stack.getItem() == Register.OBSIDIAN_SNOWBALL_STORAGE_TANK || stack.getItem() == Register.STONE_SNOWBALL_STORAGE_TANK;
    }

    protected ItemStack findAmmo(PlayerEntity player) {
        for (int j = 0; j < player.getInventory().size(); j++) {
            if (isAmmoTank(player.getInventory().getStack(j))) {
                return player.getInventory().getStack(j);
            }
        }
        return new ItemStack(Items.AIR);
    }

    @Override
    public void usageTick(World world, LivingEntity user, ItemStack stack, int remainingUseTicks) {
        if (user instanceof PlayerEntity player) {
            float pitch = player.getPitch();
            float yaw = player.getYaw();
            if (timer % 3 == 0) {
                ItemStack itemStack = findAmmo(player);
                if (itemStack.getItem() != Items.AIR) {
                    AdvancedSnowballEntity snowballEntity;
                    if (itemStack.getItem() == Register.STONE_SNOWBALL_STORAGE_TANK) {
                        snowballEntity = new AdvancedSnowballEntity(world, user, 2, 3.0F, 4.0F);
                        snowballEntity.setItem(new ItemStack(Register.STONE_SNOWBALL));
                        j = 0.1F;
                    } else if (itemStack.getItem() == Register.COMPACTED_SNOWBALL_STORAGE_TANK) {
                        snowballEntity = new AdvancedSnowballEntity(world, user, 1);
                        snowballEntity.setItem(new ItemStack(Register.COMPACTED_SNOWBALL));
                        j = 0.08F;
                    } else if (itemStack.getItem() == Register.GLASS_SNOWBALL_STORAGE_TANK) {
                        snowballEntity = new AdvancedSnowballEntity(world, user, 3, 4.0F, 5.0F);
                        snowballEntity.setItem(new ItemStack(Register.GLASS_SNOWBALL));
                        j = 0.1F;
                    } else if (itemStack.getItem() == Register.ICE_SNOWBALL_STORAGE_TANK) {
                        snowballEntity = new AdvancedSnowballEntity(world, user, 9, 4.0F, 6.0F);
                        snowballEntity.setItem(new ItemStack(Register.ICE_SNOWBALL));
                        snowballEntity.frozenTicks = 60;
                        j = 0.1F;
                    } else if (itemStack.getItem() == Register.IRON_SNOWBALL_STORAGE_TANK) {
                        snowballEntity = new AdvancedSnowballEntity(world, user, 4, 5.0F, 7.0F);
                        snowballEntity.setItem(new ItemStack(Register.IRON_SNOWBALL));
                        j = 0.12F;
                    } else if (itemStack.getItem() == Register.GOLD_SNOWBALL_STORAGE_TANK) {
                        snowballEntity = new AdvancedSnowballEntity(world, user, 5, 6.0F, 7.0F);
                        snowballEntity.setItem(new ItemStack(Register.GOLD_SNOWBALL));
                        j = 0.14F;
                    } else if (itemStack.getItem() == Register.OBSIDIAN_SNOWBALL_STORAGE_TANK) {
                        snowballEntity = new AdvancedSnowballEntity(world, user, 6, 7.0F, 8.0F);
                        snowballEntity.setItem(new ItemStack(Register.OBSIDIAN_SNOWBALL));
                        j = 0.17F;
                    } else {
                        snowballEntity = new AdvancedSnowballEntity(world, user, 7, 3.0F, 5.0F);
                        snowballEntity.setItem(new ItemStack(Register.EXPLOSIVE_SNOWBALL));
                        snowballEntity.explode = true;
                        j = 0.12F;
                    }
                    snowballEntity.punch = 1.2;
                    snowballEntity.setVelocity(user, pitch, yaw, 0.0F, 2.6F, 1.0F);
                    world.playSound(null, user.getX(), user.getY(), user.getZ(), Register.SNOWBALL_MACHINE_GUN_SHOOT, SoundCategory.PLAYERS, 1.0F, 1.0F / (world.getRandom().nextFloat() * 0.4F + 1.2F) + 0.5F);
                    world.spawnEntity(snowballEntity);
                    Vec3d cameraVec = new Vec3d(-MathHelper.cos(pitch * MathHelper.RADIANS_PER_DEGREE) * MathHelper.sin(yaw * MathHelper.RADIANS_PER_DEGREE), -MathHelper.sin(pitch * MathHelper.RADIANS_PER_DEGREE), MathHelper.cos(pitch * MathHelper.RADIANS_PER_DEGREE) * MathHelper.cos(yaw * MathHelper.RADIANS_PER_DEGREE));
                    player.addVelocity(-0.24 * j * cameraVec.x, -0.24 * j * cameraVec.y, -0.24 * j * cameraVec.z);
                    if (!player.getAbilities().creativeMode) {
                        itemStack.damage(1, player, (p) -> {
                            itemStack.decrement(1);
                            p.getInventory().offer(new ItemStack(Register.EMPTY_SNOWBALL_STORAGE_TANK), true);
                        });
                        stack.setDamage(stack.getDamage() + 1);
                        if (stack.getDamage() == 512) {
                            stack.decrement(1);
                            world.playSound(null, player.getX(), player.getY(), player.getZ(), SoundEvents.ENTITY_ITEM_BREAK, SoundCategory.NEUTRAL, 1.0F, 1.0F / (world.getRandom().nextFloat() * 0.4F + 0.8F));
                        }
                    }
                } else {
                    j = 0;
                }
            }
            if (pitch > -90.0F) {
                player.setPitch(pitch - j);
            }
            timer++;
        }
    }

    @Override
    public void appendTooltip(ItemStack stack, @Nullable World world, List<Text> tooltip, TooltipContext context) {
        tooltip.add(new TranslatableText("snowball_machine_gun.tooltip").formatted(Formatting.GRAY));
    }

    @Override
    public boolean canRepair(ItemStack stack, ItemStack ingredient) {
        return ingredient.isOf(Items.IRON_INGOT);
    }
}
